package com.jh.fcsm.controller.sys;

import com.github.pagehelper.PageInfo;
import com.jh.fcsm.beans.sys.SysRole;
import com.jh.fcsm.beans.sys.vo.RoleAndRoleTypesVO;
import com.jh.fcsm.beans.sys.vo.SysRoleExportVo;
import com.jh.fcsm.beans.sys.vo.SysRoleVo;
import com.jh.fcsm.common.BaseController;
import com.jh.fcsm.common.RestApiResponse;
import com.jh.fcsm.common.annotation.RepeatSubAnnotation;
import com.jh.fcsm.common.annotation.SystemLogAnnotation;
import com.jh.fcsm.service.sys.RoleService;
import com.jh.fcsm.util.poi.EasyExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色 controller
 *
 * @author szx
 */
@RestController
@RequestMapping("/sys/role")
@Api(value = "角色管理", tags = "角色接口")
public class RoleController extends BaseController {

    private static final String prefix = "btn:sys:role:";
    @Autowired
    private RoleService roleService;

    @RepeatSubAnnotation
    @PostMapping(value = "/addRole")
    @ApiOperation(value = "保存角色", notes = "保存角色")
    @SystemLogAnnotation(type = "保存角色", value = "保存角色")
    @PreAuthorize("hasAuthority('" + prefix + "add')")
    public RestApiResponse<?> addRole(@RequestBody SysRoleVo sysRole) throws Exception {
        SysRole role = roleService.saveOrUpdateRole(sysRole);
        return RestApiResponse.ok(role);
    }

    @RepeatSubAnnotation
    @PostMapping(value = "/updateRole")
    @ApiOperation(value = "更新角色", notes = "更新角色")
    @SystemLogAnnotation(type = "更新角色", value = "更新角色")
    @PreAuthorize("hasAuthority('" + prefix + "edit')")
    public @ResponseBody
    RestApiResponse<?> updateRole(@RequestBody SysRoleVo sysRole) throws Exception {
        return RestApiResponse.ok(this.roleService.saveOrUpdateRole(sysRole));
    }

    /**
     * 批量删除角色
     *
     * @param roleIds 角色Ids
     * @return 结果
     */
    @RepeatSubAnnotation
    @GetMapping(value = "/deleteRole/{roleIds}")
    @ApiOperation(value = "批量删除角色", notes = "批量删除角色")
    @ResponseBody
    @PreAuthorize("hasAuthority('" + prefix + "delete')")
    public RestApiResponse<?> deleteRole(@PathVariable("roleIds") String roleIds) {
        roleService.deleteRole(roleIds);
        return RestApiResponse.ok();
    }

    /**
     * 查询角色列表
     *
     * @param sysRoleVo 角色Vo
     * @return 结果
     */
    @PostMapping(value = "/listPage")
    @ApiOperation(value = "查询角色(分页)", notes = "角色名称模糊查询角色(分页)")
    @PreAuthorize("hasAuthority('" + prefix + "query')")
    public RestApiResponse<?> listRolePage(@RequestBody SysRoleVo sysRoleVo) {
        sysRoleVo.setAdmin(isAdmin());
        PageInfo<RoleAndRoleTypesVO> roleTypeVo = this.roleService.listPageRoleAndRoleTypeVo(sysRoleVo);
        return RestApiResponse.ok(roleTypeVo);
    }

    /**
     * 获取登录用户所属角色
     *
     * @return 角色
     */
    @PostMapping(value = "/listRoleByLoginUser")
    @ApiOperation(value = "查询当前用户添加的角色列表", notes = "查询当前用户添加的角色列表")
    public RestApiResponse<?> listRoleByLoginUser() {
        SysRoleVo sysRoleVo = new SysRoleVo();
        sysRoleVo.setAdmin(isAdmin());
        List<SysRole> roleList = this.roleService.listRoleByLoginUser(sysRoleVo);
        return RestApiResponse.ok(roleList);
    }

    /**
     * 根据角色ID查询角色
     *
     * @param id 角色ID
     * @return 结果
     */
    @GetMapping(value = "/findById/{id}")
    @ApiOperation(value = "根据角色ID查询角色", notes = "根据角色ID查询角色")
    @PreAuthorize("hasAuthority('" + prefix + "byid')")
    public RestApiResponse<?> findById(@PathVariable("id") String id) {
        SysRoleVo sysRole = roleService.findById(id);
        return RestApiResponse.ok(sysRole);
    }

    /**
     * 启用/禁用角色
     *
     * @param roleIds 角色ID
     * @param status  状态
     * @return 结果
     */
    @RepeatSubAnnotation
    @GetMapping(value = "/enable/{roleIds}/{status}")
    @ApiOperation(value = "改变角色状态-启用", notes = "改变角色状态-启用")
    @PreAuthorize("hasAuthority('" + prefix + "enable')")
    public @ResponseBody
    RestApiResponse<?> enable(@PathVariable("roleIds") String roleIds, @PathVariable("status") Integer status) {
        roleService.changeStatus(roleIds, status);
        return RestApiResponse.ok();

    }
    /**
     * 启用/禁用 角色动态验证
     *
     * @param roleIds 角色ID
     * @param status  状态
     * @return 结果
     */
    @RepeatSubAnnotation
    @GetMapping(value = "/enableDynamicVer/{roleIds}/{status}")
    @ApiOperation(value = "改变角色动态验证状态-启用", notes = "改变角色动态验证状态-启用")
    @PreAuthorize("hasAuthority('" + prefix + "enable')")
    public @ResponseBody
    RestApiResponse<?> enableDynamicVer(@PathVariable("roleIds") String roleIds, @PathVariable("status") Integer status) {
        roleService.enableDynamicVer(roleIds, status);
        return RestApiResponse.ok();

    }


    /**
     * 导出
     *
     * @param sysRoleVo 角色Vo
     */
    @PostMapping(value = "/export")
    @ApiOperation(value = "导出角色", notes = "导出角色")
    @PreAuthorize("hasAuthority('" + prefix + "export')")
    public void export(SysRoleVo sysRoleVo) {
        sysRoleVo.setAdmin(isAdmin());
        List<SysRoleExportVo> roleList = roleService.export(sysRoleVo);
        EasyExcelUtils.exportExcel(roleList, "导出角色结果", SysRoleExportVo.class, "导出角色结果.xlsx", response);
    }

}
